//
//  RBCategoryVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/5/26.
//

#import "RBCategoryVC.h"
#import "RBSearchVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBPager.h"
#import <UINavigationController+FDFullscreenPopGesture.h>
#import "RanTool.h"
#import "UIViewController+RBAdd.h"
#import "RBCategoryTagView.h"
#import "RBCategoryTypeView.h"
#import "RBCategoryGoodsCell.h"
#import "RBCategoryGoodsHeaderView.h"
#import "RBCollectionReusableView.h"
#import "RBCateListVC.h"
#import "RBCatResponse.h"
#import "UIViewController+RBAdd.h"
#import "RBEmptyView.h"
#import "RBGoodsDetailVC.h"

@interface RBCategoryVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property(nonatomic, strong)IBOutlet UIView *searchView;
@property(nonatomic, strong)RBCategoryTagView *tagView;
@property(nonatomic, strong)RBCategoryTypeView *typeView;
@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)NSMutableArray *dataSource;

@property(nonatomic, strong) NSArray *firstCatData;
@property(nonatomic, assign) NSInteger firstCatIndex;
@property(nonatomic, strong) NSArray *secondCatData;

@end

@implementation RBCategoryVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self getFirstCatData];
}

#pragma mark - Private

- (void)setup
{
    self.fd_prefersNavigationBarHidden = YES;
    [self.searchView zoomSubViewsWithSelf];
    self.searchView.height = Zoom(self.searchView.height);
}

- (void)fillGoodsDataWithIndex: (NSInteger)index
{
    [self.collectionView hidePlaceHolder];
    [self.dataSource removeAllObjects];
    
    if(index >= self.secondCatData.count)
    {
        [self.collectionView reloadData];
        return;
    }
    RBCatModel *model = self.secondCatData[index];
    if(!model.son.count)
    {
        [self.collectionView showPlaceHolder:RB_EMPTY_TIP tapBlock:nil];
        [self.collectionView reloadData];
        return;
    }
    
    __weak typeof(self) weakSelf = self;
    for (RBCatModel *catModel in model.son)
    {
        [self.dataSource addObject:({
            RBGroupModel *group = RBGroupModel.new;
            group.headerReuseIdentifier = NSStringFromClass(RBCollectionReusableView.class);
            group.headerReferenceSize = CGSizeMake(Zoom(230), Zoom(47));
            group.headerView = [RBCategoryGoodsHeaderView viewWithTitle:catModel.cate_name block:^{
                
            }];
            group.sectionInset = UIEdgeInsetsMake(0, 0, Zoom(30), 0);
            group.minimumLineSpacing = Zoom(12);
            if(catModel.goods.count)
            {
                group.cellModels = ({
                    NSMutableArray *arrayM = NSMutableArray.array;
                    for (RBCatGoodsModel *goodsModel in catModel.goods)
                    {
                        [arrayM addObject:({
                            RBCellModel *cellModel = RBCellModel.new;
                            cellModel.reuseCellIdentifier = NSStringFromClass(RBCategoryGoodsCell.class);
                            cellModel.itemSize = CGSizeMake(Zoom(72), Zoom(95));
                            cellModel.cellData = goodsModel;
                            cellModel.cellSelectBlock = ^(NSIndexPath *indexPath) {
                                RBCatGoodsModel *model = [RanTool modelWithSection:indexPath.section row:indexPath.row dataSource:weakSelf.dataSource];
                                RBGoodsDetailVC *vc = RBGoodsDetailVC.new;
                                vc.id = model.id;
                                [weakSelf.navigationController pushViewController:vc animated:YES];
                            };
                            cellModel;
                        })];
                    }
                    arrayM;
                });
            }
            else
            {
                group.footerReuseIdentifier = NSStringFromClass(RBCollectionReusableView.class);
                group.footerReferenceSize = CGSizeMake(Zoom(230), Zoom(60));
                group.footerView = RBEmptyView.new;

            }
            group;
        })];
    }
    [self.collectionView reloadData];
}

#pragma mark - Network

- (void)getFirstCatData
{
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        
    } path:@"api/first_list" post:YES log:YES responseClass:RBCatResponse.class desView:self.view showHud:YES showPlaceHolder:YES sucBlock:^(RBCatResponse *data) {
        weak_self.firstCatData = data.data;
        
        [weak_self.view addSubview:weak_self.searchView];
        [weak_self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.mas_equalTo(kRBStatusBarHeight + 15);
            make.height.mas_equalTo(weak_self.searchView.height);
        }];
        
        [weak_self.tagView setTags:({
            NSMutableArray *arrayM = NSMutableArray.array;
            for (RBCatModel *model in data.data) {
                [arrayM addObject:model.cate_name];
            }
            arrayM;
        })];
        
        if(weak_self.firstCatData.count)
        {
            weak_self.firstCatIndex = 0;
        }
        
    } failBlock:nil reloadBlock:^{
        [weak_self getFirstCatData];
    }];
}

- (void)getSecondCatData
{
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        RBCatModel *model = self.firstCatData[self.firstCatIndex];
        dicM[@"pid"] = model.id;
    } path:@"api/goods_list" post:YES log:YES responseClass:RBCatResponse.class desView:self.view showHud:YES showPlaceHolder:YES sucBlock:^(RBCatResponse *data) {
        weak_self.secondCatData = data.data;
        weak_self.typeView.data = weak_self.secondCatData;
        [weak_self fillGoodsDataWithIndex:0];
    } failBlock:^(RBCatResponse *data) {
        [weak_self.view showTip:data.message completion:nil];
    } reloadBlock:nil];
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

kRBCollectionViewDataSourceAndDelegate(self.dataSource);

#pragma mark - Event

- (IBAction)searchViewTapped:(id)sender
{
    RBSearchVC *vc = RBSearchVC.new;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Setter

- (void)setFirstCatIndex:(NSInteger)firstCatIndex
{
    _firstCatIndex = firstCatIndex;
    [self getSecondCatData];
}

#pragma mark - Getter

- (RBCategoryTagView *)tagView
{
    if (!_tagView)
    {
        self.tagView = ({
            RBCategoryTagView *view = RBCategoryTagView.new;
            __weak typeof(self) weak_self = self;
            view.selectBlock = ^(NSInteger index) {
                [weak_self setFirstCatIndex:index];
            };
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_equalTo(0);
                make.top.mas_equalTo(self.searchView.mas_bottom).offset(Zoom(13));
            }];
            view;
        });
    }
    return _tagView;
}

- (RBCategoryTypeView *)typeView
{
    if (!_typeView)
    {
        self.typeView = ({
            RBCategoryTypeView *view = RBCategoryTypeView.new;
            __weak typeof(self) weak_self = self;
            view.selectBlock = ^(NSInteger index) {
                [weak_self fillGoodsDataWithIndex:index];
            };
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.bottom.mas_equalTo(0);
                make.top.mas_equalTo(self.tagView.mas_bottom).offset(Zoom(13));
                make.width.mas_equalTo(Zoom(115));
            }];
            view;
        });
    }
    return _typeView;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        self.collectionView = ({
            UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new;;
            UICollectionView *view = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout];
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            view.dataSource = self;
            view.delegate = self;
            view.backgroundColor = UIColor.clearColor;
            kRBIgnoreScrollViewInsetsAdjust(view);
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(Zoom(230));
                make.right.mas_equalTo(Zoom(-12));
                make.top.bottom.mas_equalTo(self.typeView);
            }];
            view;
        });
    }
    return _collectionView;
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource)
    {
        self.dataSource = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
    }
    return _dataSource;
}

@end
